package ocr.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client extends Thread {

	private static String SERVER_NAME = "133.27.79.11";
	private static int PORT = 6666;

	boolean connection_status = true;
	String connection_error = "";

	Socket socket;

	DataInputStream input;
	DataOutputStream output;

	File file;
	String lang;

	String character = null;

	public Client(File file, String lang) {
		// TODO Auto-generated constructor stub
		this.file = file;
		this.lang = lang;

		try {
			socket = new Socket(SERVER_NAME, PORT);

			if (socket != null) {
				output = new DataOutputStream(socket.getOutputStream());
				input = new DataInputStream(socket.getInputStream());
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			connection_status = false;
			connection_error = "unknown host exception";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			connection_status = false;
			connection_error = "io exception";
		}

		// this.start();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		if (connection_status) {
			// send data
			sendLang(lang);
			sendPicture(file);

			// receive result
			character = receiver();

			// close connection
			close();
		}
	}

	/*
	 * send language
	 */
	public void sendLang(String lang) {
		try {
			output.writeUTF(lang);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * send picture
	 */
	public void sendPicture(File file) {
		FileInputStream fin;
		try {
			fin = new FileInputStream(file);
			int size = fin.available();

			// send file name
			output.writeUTF(file.getName());
			// send picture size
			output.writeInt(size);

			// send picture data
			int buffer_size = 4000, sended = 0, temp;
			byte[] data = new byte[buffer_size];
			while (sended < size) {
				if (size - sended >= buffer_size) {
					temp = fin.read(data);
				} else {
					temp = fin.read(data, 0, size - sended);
				}
				output.write(data, 0, temp);

				sended += temp;
			}

			fin.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * receive result
	 */
	public String receiver() {
		String result = null;
		try {
			result = input.readUTF();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	/*
	 * 
	 */
	public String getCharacter() {
		return character;
	}

	/*
	 * close connection
	 */
	public void close() {
		try {
			output.close();
			input.close();
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/*
	 * do ocr
	 */
	public String doOCR() {

		if (connection_status) {
			// send picture
			sendLang(lang);
			sendPicture(file);

			// receive result
			character = receiver();

			// close connection
			close();
		}
		return character;
	}

	public static void main(String[] args) {
		File file = new File("eurotext.gif");
		// System.out.println(file.getName());
		if (file != null) {
			System.out.println("Data ok:");
			Client client = new Client(file, "ENGLISH");
			System.out.println(client.doOCR());
		}
	}
}
